export default {
  install(Vue) {
    const eventBus = new Vue({
      methods: {
        on(eventName, callback) {
          // 注册事件监听器
          if (!this.events[eventName]) {
            this.events[eventName] = [];
          }
          this.events[eventName].push(callback);
        },
        off(eventName, callback) {
          // 移除事件监听器
          if (this.events[eventName]) {
            let index = this.events[eventName].indexOf(callback);
            if (index !== -1) {
              this.events[eventName].splice(index, 1);
            }
          }
        },
        emit(eventName, ...args) {
          // 触发事件
          if (this.events[eventName]) {
            this.events[eventName].forEach((callback) => {
              callback.apply(this, args);
            });
          }
        },
      },
      beforeCreate() {
        this.events = {};
      },
    });

    Vue.mixin({
      beforeCreate() {
        this.$events = eventBus;
      },
    });
  },
};